BUILD_TOP_DIR=$(shell pwd)
PRODUCT_FLAG="Shinbada Platform Cloud "
PROJECT_VERSION="1.2.10"

# param MODLE_NAME              模块名称，即部署在远程服务器主目录名称
# param STARTER_JAR_NAME        启动器的jar文件名称
# param STARTER_SOURCE_NAME     启动器模块目录名
# param RELEASE_DIR             打包后的目录
# param BUILD_APP_DIR           打包app目录
# param DEPLOY_IP               部署远程服务器的ip
# param DEPLOY_LIST             部署远程服务器的目录
# param DEPLOY_MODEL            部署模式，即maven环境隔离所指定的profiles

MODLE_NAME=competitor
STARTER_JAR_NAME=s-competitor
STARTER_SOURCE_NAME=starter
RELEASE_DIR=${BUILD_TOP_DIR}/release/
BUILD_APP_DIR=${RELEASE_DIR}/${MODLE_NAME}

DEPLOY_IP=shin39
DEPLOY_LIST=/app/
DEPLOY_MODEL=test
    
clean:
	@echo "====================================================="
	@echo "Clean Build Environment ..."
	@echo "====================================================="
	-rm -rf ${RELEASE_DIR}
	mvn clean
    
init: clean
	@echo "====================================================="
	@echo "Init Build Environment ..."
	@echo "====================================================="
	mkdir -p ${BUILD_APP_DIR}
        
compile:
	# Compile JAR files
#	mvn versions:set -DoldVersion=* -DnewVersion=${PROJECT_VERSION} -DprocessAllModules=true
#	mvn versions:commit
	mvn install -Dmaven.test.skip=true -P${DEPLOY_MODEL}
	# 回退pom的备份文件
#	mvn versions:revert

package:
	mvn package -Dmaven.test.skip=true -P${DEPLOY_MODEL}

deploy:
#	mvn versions:set -DoldVersion=* -DnewVersion=${PROJECT_VERSION} -DprocessAllModules=true
#	mvn versions:commit
	mvn deploy -s ./settings.xml -Dmaven.test.skip=true

    
bundle:
	@echo "====================================================="
	@echo "Start Building Tar Package ..."
	@echo "====================================================="

	# Copy binarys files
	@echo "====================================================="
	@echo "Start Copying APP lib files ..."
	@echo "====================================================="
	cp -rf ${BUILD_TOP_DIR}/${STARTER_SOURCE_NAME}/src/main/resources/bin/* ${BUILD_APP_DIR}/
	chmod +x ${BUILD_APP_DIR}/*.sh

	#Copy jar
	cp ${BUILD_TOP_DIR}/${STARTER_SOURCE_NAME}/target/${STARTER_JAR_NAME}.jar ${BUILD_APP_DIR}/

daily: init compile package bundle
	@echo "====================================================="
	@echo "Build Daily Package Successfully !"
	@echo "====================================================="

core_release: init deploy bundle
	@echo "====================================================="
	@echo "Build Core Release Package Successfully !"
	@echo "====================================================="

deploy_remote: init package bundle
	@echo "====================================================="
	@echo "Starting Deploy_remote ...."
	@echo "====================================================="
	ssh root@${DEPLOY_IP} "cd ${DEPLOY_LIST}${MODLE_NAME}/; ./stop.sh"
	sleep 2s
	# clean remote bin
	ssh root@${DEPLOY_IP} "rm -rf ${DEPLOY_LIST}${MODLE_NAME}/*.sh"
	# app backend send remote
	scp -r ${BUILD_APP_DIR} root@${DEPLOY_IP}:${DEPLOY_LIST}
	@echo "================= Start Services ====================="
	ssh root@${DEPLOY_IP} "cd ${DEPLOY_LIST}${MODLE_NAME}/; ./start.sh ${DEPLOY_MODEL}"

	@echo "====================================================="
	@echo " Starting Deploy_Remote Successfully!"
	@echo "====================================================="

help:
	@echo "====================================================="
	@echo "Usage:               Shinbada"
	@echo "make clean           Clean all build"
	@echo "make init            Init Build Environment"
	@echo "make compile         Build everything and Install"
	@echo "make package         Build everything and pack into tar package"
	@echo "make deploy          Build and deploy remote maven"
	@echo "make bundle          "
	@echo "make daily           "
	@echo "make core_release    Build release deploy"
	@echo "make deploy_remote   Build and deploy remote"
	@echo "====================================================="
